package com.commerce.product.controller;

import com.commerce.common.entity.ProductReview;
import com.commerce.common.utils.PageResult;
import com.commerce.common.utils.Result;
import com.commerce.product.service.ReviewService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 商品评价控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/reviews")
@CrossOrigin
public class ReviewController {

    @Autowired
    private ReviewService reviewService;

    /**
     * 获取商品评价列表
     */
    @GetMapping("/product/{productId}")
    public Result<PageResult<ProductReview>> getProductReviews(
            @PathVariable Long productId,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) Integer rating,
            @RequestParam(required = false) Boolean withImages) {
        try {
            PageResult<ProductReview> pageResult = reviewService.getProductReviews(productId, page, size, rating, withImages);
            return Result.success(pageResult);
        } catch (Exception e) {
            log.error("获取商品评价列表失败", e);
            return Result.error("获取商品评价列表失败：" + e.getMessage());
        }
    }

    /**
     * 获取商品评价统计
     */
    @GetMapping("/product/{productId}/stats")
    public Result<Map<String, Object>> getProductReviewStats(@PathVariable Long productId) {
        try {
            Map<String, Object> stats = reviewService.getProductReviewStats(productId);
            return Result.success(stats);
        } catch (Exception e) {
            log.error("获取商品评价统计失败", e);
            return Result.error("获取商品评价统计失败：" + e.getMessage());
        }
    }

    /**
     * 添加商品评价
     */
    @PostMapping
    public Result<Boolean> addReview(@RequestBody ProductReview review) {
        try {
            boolean success = reviewService.addReview(review);
            return Result.success(success);
        } catch (Exception e) {
            log.error("添加商品评价失败", e);
            return Result.error("添加商品评价失败：" + e.getMessage());
        }
    }

    /**
     * 获取用户评价列表
     */
    @GetMapping("/user/{userId}")
    public Result<PageResult<ProductReview>> getUserReviews(
            @PathVariable Long userId,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        try {
            PageResult<ProductReview> pageResult = reviewService.getUserReviews(userId, page, size);
            return Result.success(pageResult);
        } catch (Exception e) {
            log.error("获取用户评价列表失败", e);
            return Result.error("获取用户评价列表失败：" + e.getMessage());
        }
    }

    /**
     * 商家回复评价
     */
    @PutMapping("/{id}/reply")
    public Result<Boolean> replyReview(@PathVariable Long id, @RequestBody Map<String, String> params) {
        try {
            String replyContent = params.get("replyContent");
            boolean success = reviewService.replyReview(id, replyContent);
            return Result.success(success);
        } catch (Exception e) {
            log.error("回复评价失败", e);
            return Result.error("回复评价失败：" + e.getMessage());
        }
    }

    /**
     * 检查用户是否可以评价该商品
     */
    @GetMapping("/check")
    public Result<Boolean> checkCanReview(
            @RequestParam Long userId,
            @RequestParam Long productId,
            @RequestParam Long orderId) {
        try {
            boolean canReview = reviewService.checkCanReview(userId, productId, orderId);
            return Result.success(canReview);
        } catch (Exception e) {
            log.error("检查评价权限失败", e);
            return Result.error("检查评价权限失败：" + e.getMessage());
        }
    }
}
